#include "game.h"
#include "display.c"
#include "btns.c"
#include "uart.c"
#include "timer.c"

void init_shape()
{
    update_timer_seed();
    game_shape.pos = timer_rand() % 4;
    game_shape.type = timer_rand() % 7;
    game_shape.shape = TETRIS_SHAPES[game_shape.type][game_shape.pos];
    update_timer_seed();
    game_shape.next_pos = timer_rand() % 4;
    game_shape.next_type = timer_rand() % 7;
    game_shape.next_shape = TETRIS_SHAPES[game_shape.next_type][game_shape.next_pos];
}

/**
 * 使用上下左右按钮控制方块的移动
 * 使用开始按钮，切换到下一个方块
 */
void main()
{
    unsigned char btn_code;
    unsigned char current_page = 1;  // 当前方块显示的页面位置
    unsigned char current_col = 10;  // 当前方块显示的列位置

    // 初始化各个模块
    uart_init();
    timer0_init();
    display_init();

    // 初始化方块
    init_shape();

    // 显示当前方块和下一个方块
    display_shape(current_page, current_col, game_shape.shape);
    display_shape(4, 10, game_shape.next_shape);

    while (1)
    {
        btn_code = btn_scan();
        switch (btn_code)
        {
        case BTN_CODE_UP:
            // 上移：清除当前位置，向上移动
            display_shape(current_page, current_col, 0x0000); // 清除当前方块
            if (current_page > 0) {
                current_page--;
            }
            display_shape(current_page, current_col, game_shape.shape);
            break;

        case BTN_CODE_DOWN:
            // 下移：清除当前位置，向下移动
            display_shape(current_page, current_col, 0x0000); // 清除当前方块
            if (current_page < 6) { // 防止超出屏幕范围
                current_page++;
            }
            display_shape(current_page, current_col, game_shape.shape);
            break;

        case BTN_CODE_LEFT:
            // 左移：清除当前位置，向左移动
            display_shape(current_page, current_col, 0x0000); // 清除当前方块
            if (current_col > 0) {
                current_col -= 4; // 每次移动4个像素列
            }
            display_shape(current_page, current_col, game_shape.shape);
            break;

        case BTN_CODE_RIGHT:
            // 右移：清除当前位置，向右移动
            display_shape(current_page, current_col, 0x0000); // 清除当前方块
            if (current_col < 112) { // 防止超出屏幕范围 (128-16=112)
                current_col += 4; // 每次移动4个像素列
            }
            display_shape(current_page, current_col, game_shape.shape);
            break;

        case BTN_CODE_START:
            // 开始按钮：切换到下一个方块
            // 将next方块设为当前方块
            game_shape.shape = game_shape.next_shape;
            game_shape.type = game_shape.next_type;
            game_shape.pos = game_shape.next_pos;

            // 生成新的next方块
            update_timer_seed();
            game_shape.next_pos = timer_rand() % 4;
            game_shape.next_type = timer_rand() % 7;
            game_shape.next_shape = TETRIS_SHAPES[game_shape.next_type][game_shape.next_pos];

            // 清除并重新显示方块
            display_shape(current_page, current_col, 0x0000); // 清除当前位置
            display_shape(current_page, current_col, game_shape.shape); // 显示新的当前方块
            display_shape(4, 10, 0x0000); // 清除next方块位置
            display_shape(4, 10, game_shape.next_shape); // 显示新的next方块
            break;

        default:
            break;
        }
    }
}